#include "writeLightDirectional.hpp"
#include "MQ3DMath.hpp"
#include "Log.hpp"
using namespace std;
using namespace m3g;


unsigned int writeLightDirectional (M3GWriter* writer, MQScene mq_scene)
{
	int directional_color = 0x00ffffff;
	MQPoint z   = MQPoint(0,0,-1);
	MQPoint lit = mq_scene->GetGlobalDirectionalLight();
	lit.normalize();

	float   angle = acosf(dot(z,lit))/(2*PI)*360;
	MQPoint axis  = cross (z, lit);
	axis.normalize();
    
	M3GObject3DStruct      obj;
	M3GTransformableStruct tra;
	M3GNodeStruct          nod;
	M3GLightStruct         lgh;
	tra.has_component_transform = true;
	tra.orientation_angle       = angle;
	tra.orientation_axis[0]     = axis.x;
	tra.orientation_axis[1]     = axis.y;
	tra.orientation_axis[2]     = axis.z;
	lgh.mode                    = M3GLightStruct:: DIRECTIONAL;
	lgh.color                   = directional_color;
	lgh.intensity               = 1;

	unsigned int object_index;

	writer->startObject       (M3G_TYPE_LIGHT, &object_index);
	writer->writeObject3D     (obj);
	writer->writeTransformable(tra);
	writer->writeNode         (nod);
	writer->writeLight        (lgh);
	writer->endObject         ();

   	Log:: print (" [%d] Write Light(directional) :", object_index);
    Log:: print ("  color=0x%08x, ", directional_color);
    Log:: print (", direction=%f,%f,%f.\n", lit.x, lit.y, lit.z);

	return object_index;
}